/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ServerGUI;

import Communication.M2ServerPacket;
import Const.ModuleConst;
import java.io.IOException;
import java.io.ObjectOutputStream;


/**
 *
 * @author cypek
 */
public class ServerGUIsender implements Runnable {

    private ServerGUIController guiController;
    //
    //
    //  TUTAJ ZMIENILEM KONSTRUKTOR @KAMIL
    //
    private M2ServerPacket greetings = new M2ServerPacket(ModuleConst.TypeGUI, 0, null);

    public ServerGUIsender(ServerGUIController guiController) {
        this.guiController = guiController;
    }


    /**
     * Get the value of guiController
     *
     * @return the value of guiController
     */
    public ServerGUIController getGuiController() {
        return guiController;
    }

    /**
     * Set the value of guiController
     *
     * @param guiController new value of guiController
     */
    public void setGuiController(ServerGUIController guiController) {
        this.guiController = guiController;
    }


    @Override
    public void run()
    {
        try {
              while(guiController.isIsServerGUIRunning())
                {
                    ObjectOutputStream oos = new ObjectOutputStream(guiController.getSocket().getOutputStream());
                    oos.writeObject(greetings);
                    oos.flush();

                    Thread.sleep(ModuleConst.TimeDelayLong);
                }        
        }catch(IOException ex){
               guiController.setIsServerGUIRunning(false);
               System.out.println("Blad podczas wysylania");
               System.out.println( "class " + ex.getClass() + " \t " + ex.getMessage() );
               while(!guiController.isIsServerGUIRunning())
               {
                   guiController.initSocket();
               }
            
        }catch(InterruptedException ex)
        {
            System.out.println("asd");
            System.out.println( "class " + ex.getClass() + " \t " + ex.getMessage() );
        }
    }

}
